# include"Bspline.h"
# include<iostream>
# include<fstream>


using namespace std;

# define Num 401

// Define function f(x)
class Fun : public Function
{
public:
    double operator() (const double & _x) const
    {
        return (1.0 / (1.0 + _x * _x));
    }
};

// Define Es32(x) for S32
class Es32 : public Function
{
private:
    vector<double> xpoint;
    Function & fun;
    Bspline B32;

public:
    Es32(const vector<double> & _xpoint, Function & _fun) :
        xpoint{_xpoint}, fun{_fun}
    {
        Bspline B(S32Car, xpoint, fun);
        B32 = B;
    }

    double operator() (const double & _x) const
    {
        double output = 0;
        output = fabs(B32.solve(_x) - fun(_x));
        return output;
    }
};

// Define Es21(x) for S21
class Es21 : public Function
{
private:
    vector<double> xpoint;
    Function & fun;
    Bspline B21;

public:
    Es21(const vector<double> & _xpoint, Function & _fun) :
        xpoint{_xpoint}, fun{_fun}
    {
        Bspline B(S21Car, xpoint, fun);
        B21 = B;
    }

    double operator() (const double & _x) const
    {
        double output = 0;
        output = fabs(B21.solve(_x) - fun(_x));
        return output;
    }
};

// Main funciton
int main()
{
    vector<double> xpoint{-3.5, -3, -0.5, 0, 0.5, 3, 3.5};
    Fun fun;

    vector<double> point32, point21;
    for(int i = 1; i <= 11; i++)
        point32.push_back(-6.0 + i);
    for(int i = 1; i <= 10; i++)
        point21.push_back(i - 5.5);
    Es32 ES3(point32, fun);
    Es21 ES2(point21, fun);

    ofstream fout;
    fout.open("result/ProDresult.txt", ios::out);
    fout << "x,    Es(x) of S21,   Es(x) of S32" << endl;

    vector<double> error32, error21;
    for(int i = 0; i < xpoint.size(); i++)
    {
        double s2 = ES2(xpoint[i]);
        double s3 = ES3(xpoint[i]);
        error21.push_back(s2);
        error32.push_back(s3);
        fout << xpoint[i] << ",     " << s2 << ",      " << s3 << endl;
    }
    fout.close();

    cout << "Finish calculating for Pro D." << endl;

    return 0;
}